package com.tlcx.kfip.adapter;

import android.content.Context;
import android.net.Uri;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.tlcx.kfip.R;
import com.tlcx.kfip.adapter.base.XRecyclerBaseAdapter;
import com.tlcx.kfip.entity.KfipListEntity;
import com.tlcx.kfip.utils.AppInfoUtils;

/**
 * 夺宝主页适配器
 * Created by victor on 2016/10/28 10:31.
 * Email:zhuzhaoyang@yuanchuangyun.com
 */

public class KfipListAdapter extends XRecyclerBaseAdapter<KfipListEntity> implements View.OnClickListener {

    private Context context;

    public KfipListAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public int getItemResource(int viewType) {
        return R.layout.adapter_kfip;
    }

    @Override
    public void bindData(BaseViewHolder holder, int position, KfipListEntity item) {
        if (item == null) {
            return;
        }
        //设置标题
        TextView titleTv = holder.getView(R.id.tv_adapter_kfip_title);
        titleTv.setText(item.getName());
        //设置icon图片
        SimpleDraweeView iconIv = holder.getView(R.id.iv_adapter_kfip_icon);
        Uri uri = Uri.parse("res://"+ AppInfoUtils.getCurrentPkgName(context)+
                "/"+item.getIconUrl());
        iconIv.setImageURI(uri);
        //设置进度
        TextView progressTv = holder.getView(R.id.tv_adapter_kfip_progress);
        ProgressBar progressBar = holder.getView(R.id.pb_kfip_progress);
        progressTv.setText(setProgressTextStyle(item.getProgress()));
        progressBar.setProgress(item.getProgress());

        //加入清单
        TextView addToBillTv = holder.getView(R.id.tv_adapter_add_to_bill);
        addToBillTv.setTag(item);
        addToBillTv.setOnClickListener(this);

    }

    /**
     * 设置进度显示文字
     */
    private Spanned setProgressTextStyle(int progress){
        StringBuffer buffer = new StringBuffer();

        buffer.append("<font color = '#999999'>");
        buffer.append(context.getString(R.string.kfip_progress_prize));
        buffer.append("</font>");

        buffer.append("<font color = '#ff1010'>");
        buffer.append(""+progress+"%");
        buffer.append("</font>");

        return Html.fromHtml(buffer.toString());

    }

    @Override
    public void onClick(View view) {
        KfipListEntity tag = (KfipListEntity) view.getTag();
    }
}
